package com.sh.web.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sh.api.configuration.ResfoodApi;
import com.sh.bean.CartItem;
import com.sh.bean.Resfood;
import com.sh.web.models.ResponseResult;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Tag(name = "购物车管理")
@RestController
@RequestMapping(value = "/rescart")
@Slf4j
public class CartController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RestTemplate restTemplate;


    @RequestMapping(value = "/addCart", method = {RequestMethod.PUT, RequestMethod.POST})
    public ResponseResult addCart(@RequestParam Integer fid, @RequestParam Integer num, HttpServletRequest request) {
        Map<String, Object> useClaims = (Map<String, Object>)request.getAttribute("userClaims");
        String userid = useClaims.get("userid").toString();
        Resfood food = getFoodInfo(userid, fid);
        Map<String, CartItem> cart = new HashMap<>();
        if ( this.redisTemplate.hasKey("cart:"+userid) ){
            cart = (Map<String, CartItem>)this.redisTemplate.opsForValue().get("cart:"+userid);
        }
        CartItem ci ;
        if ( cart.containsKey(fid + "")){
            ci = cart.get(fid + "");
            ci.setNum( ci.getNum() + num );
            cart.put(fid + "", ci);
        }else {
            ci = new CartItem();
            ci.setNum(num);
            ci.setFood(food);
            cart.put(fid + "", ci);
        }
        if (ci.getNum()<=0 ) {
            cart.remove(fid + "");
        }
        this.redisTemplate.opsForValue().set("cart:"+userid, cart);
        return ResponseResult.ok("添加成功").setData(cart);
    }

    @RequestMapping(value = "/getCartInfo", method = {RequestMethod.GET})
    public ResponseResult getCartInfo( HttpServletRequest request){
        Map<String, Object> useClaims = (Map<String, Object>)request.getAttribute("userClaims");
        String userid = useClaims.get("userid").toString();
        Map<String, CartItem> cart = (Map<String, CartItem>)this.redisTemplate.opsForValue().get("cart:"+userid);
        return ResponseResult.ok("获取成功").setData(cart);
    }

    @RequestMapping(value = "/clearAll", method = {RequestMethod.GET})
    @ApiResponse(description = "清空购物车")
    public ResponseResult clearAll( HttpServletRequest request){
        Map<String, Object> useClaims = (Map<String, Object>)request.getAttribute("userClaims");
        String userid = useClaims.get("userid").toString();
        redisTemplate.delete("cart:"+userid);
        return ResponseResult.ok("清空成功");
    }


    @PostMapping(value = "/confirmOrder")
    @ApiResponse(description = "下单")
    public ResponseResult confirmOrder( HttpServletRequest request) throws IOException {
             clearAll(request);
        return ResponseResult.ok("清空成功");
    }




    @Autowired
    private ResfoodApi resfoodApi;

    private Resfood getFoodInfo(String userid, Integer fid) {
        // 方案一：使用RestTemplate调用接口
//        Map<String, Object> result = this.restTemplate.getForObject("http://localhost:8080/resfood/findById/" + fid, Map.class);
        // 方案二：使用loadBalance调用接口
//        Map<String, Object> result =
//                this.restTemplate.getForObject("http://res-food/resfood/findById/" + fid, Map.class);
        // 方案三：使用Feign调用接口
        Map<String, Object> result = resfoodApi.findById(fid);
        ObjectMapper objectMapper = new ObjectMapper();
        Resfood food = objectMapper.convertValue(result.get("obj"), Resfood.class);
        return  food;
    }


}
